package com.csdn.chain;

import java.util.List;

public interface Plugin {
    InvokeResult invoke(TestContext context,Chain chain);

    interface Chain {
        InvokeResult proceed();
    }

    class PluginChain implements Chain {
        private final int index;
        private final TestContext context;
        private final List<Plugin> pluginList;

        public PluginChain(int index, TestContext testContext,List<Plugin> pluginList) {
            this.index = index;
            this.context = testContext;
            this.pluginList = pluginList;
        }

        @Override
        public InvokeResult proceed() {
            if (index > pluginList.size()) {
                throw new RuntimeException("链路执行失败");
            }
            PluginChain next = new PluginChain(index + 1,context ,pluginList);
            Plugin plugin = pluginList.get(index);
            return plugin.invoke(context,next);
        }
    }
}
